package pro.shushi.pamirs.demo.core.spi;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import pro.shushi.pamirs.boot.base.model.Action;
import pro.shushi.pamirs.boot.base.model.ViewAction;
import pro.shushi.pamirs.meta.common.spi.SPI;
import pro.shushi.pamirs.user.api.model.tmodel.TopBarActionGroup;
import pro.shushi.pamirs.user.api.model.tmodel.TopBarUserBlock;
import pro.shushi.pamirs.user.api.spi.TopBarUserBlockDataApi;

import java.util.*;

/**
 * 自定义扩展TopBarUserBlock
 */
@Order(10)
@Component
@SPI.Service
public class DemoTopBarUserBlockDataApi implements TopBarUserBlockDataApi {


    @Override
    public TopBarUserBlock extendData(TopBarUserBlock data) {
        List<TopBarActionGroup> actionGroups = data.getActionGroups();
        TopBarActionGroup actionGroup = new TopBarActionGroup();
        List<Action> actions = new ArrayList<>();

        //例如增加一个菜单
        ViewAction demoViewAction = new ViewAction().setName("DemoMenus_ItemPMenu_DemoItemMenu").queryOne();
        if(demoViewAction != null){

            //设置菜单的icon
            Map<String, Object> attributes = Optional.ofNullable(demoViewAction.getAttributes()).orElse(new HashMap<>());
            attributes.put("icon", "oinone-xiugaimima");
            demoViewAction.setAttributes(attributes);
            actions.add(demoViewAction);
            actionGroup.setActions(actions);
        }
        //例如增加一个菜单
        ViewAction demoViewAction2 = new ViewAction().setName("DemoMenus_ItemPMenu_DemoItemCategoryMenu").queryOne();
        if(demoViewAction2 != null){

            //设置菜单2的icon
            Map<String, Object> attributes = Optional.ofNullable(demoViewAction2.getAttributes()).orElse(new HashMap<>());
            attributes.put("icon", "oinone-xiugaimima");
            demoViewAction2.setAttributes(attributes);
            actions.add(demoViewAction2);
            actionGroup.setActions(actions);
        }
        actionGroups.add(new TopBarActionGroup().setActions(actions));
        data.setActionGroups(actionGroups);
        return data;
    }
}
